﻿using System;
using System.Data;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;


namespace Shuba.Framework
{
    [ToolboxData("<{0}:SHUBAGrid2 runat=server></{0}:SHUBAGrid2>")]

    public class SHUBAGrid : GridView
    {
        private DropDownList ddl = new DropDownList();
        private ImageButton firstLink = new ImageButton();
        private ImageButton prevLink = new ImageButton();
        private ImageButton nextLink = new ImageButton();
        private ImageButton lastLink = new ImageButton();

        public bool UseCustom { get; set; }

        public int ItemsCount { get; set; }

        public int PageCount2 { get { return (int)Math.Ceiling(ItemsCount * 1d / PageSize); } }

        protected override void OnInit(EventArgs e)
        {
            ddl.AutoPostBack = true;
            ddl.SelectedIndexChanged += new EventHandler(ddl_SelectedIndexChanged);
            firstLink.Click += new ImageClickEventHandler(firstLink_Click);
            prevLink.Click += new ImageClickEventHandler(prevLink_Click);
            nextLink.Click += new ImageClickEventHandler(nextLink_Click);
            lastLink.Click += new ImageClickEventHandler(lastLink_Click);


            firstLink.CssClass = "icon_first";
            prevLink.CssClass = "icon_previous";
            nextLink.CssClass = "icon_next";
            lastLink.CssClass = "icon_last";

            firstLink.ImageUrl = "~/images/trans.gif";
            prevLink.ImageUrl = "~/images/trans.gif";
            nextLink.ImageUrl = "~/images/trans.gif";
            lastLink.ImageUrl = "~/images/trans.gif";


            base.RowStyle.CssClass = "bg_grid";
            base.EmptyDataRowStyle.CssClass = "bg_grid2";
            base.AlternatingRowStyle.CssClass = "bg_grid2";
            base.HeaderStyle.CssClass = "bg_title";
            base.HeaderStyle.ForeColor = System.Drawing.Color.White;//05 okt 2011

            base.FooterStyle.CssClass = "bg_footer";
            base.PagerStyle.CssClass = "bg_footer";
            base.PagerStyle.HorizontalAlign = HorizontalAlign.Right;
            base.CssClass = "text";
            base.CellPadding = 2;
            base.BorderWidth = 1;
            //base.PageSize = 20;

            ddl.CssClass = "text";

            base.OnInit(e);
        }



        private void BuildPageControl(int pgCount, GridViewRowEventArgs e)
        {
            //create custom paging
            if (pgCount > 1)
            {
                ddl.Items.Clear();
                for (int i = 1; i <= pgCount; i++)
                    if (ddl.Items.FindByValue(i.ToString()) == null)
                        ddl.Items.Add(new ListItem(i.ToString()));

                
                if (UseCustom)
                {
                    base.FooterRow.Cells[0].Controls.Clear();
                }
                else
                {
                    e.Row.Cells[0].Controls.Clear();
                }
                Table oTab = new Table();
                oTab.Width = Unit.Percentage(100);
                TableRow oRow = new TableRow();
                oTab.Rows.Add(oRow);

                if (PageIndex == 0)
                {
                    firstLink.Visible = false;
                    prevLink.Visible = false;
                }
                else
                {
                    firstLink.Visible = true;
                    prevLink.Visible = true;
                }

                if (pgCount == PageIndex + 1)
                {
                    nextLink.Visible = false;
                    lastLink.Visible = false;
                }
                else
                {
                    nextLink.Visible = true;
                    lastLink.Visible = true;
                }

                TableCell oCell = new TableCell();
                oCell.Controls.Add(firstLink);
                oRow.Cells.Add(oCell);
                oCell = new TableCell();
                oCell.Controls.Add(prevLink);
                oRow.Cells.Add(oCell);

                oCell = new TableCell();
                oCell.VerticalAlign = VerticalAlign.Top;
                Label lbl = new Label();

                lbl.Text = "&nbsp;Page&nbsp;" + (PageIndex + 1).ToString() + "&nbsp;of&nbsp;" + pgCount.ToString() + "&nbsp;";
                lbl.CssClass = ddl.CssClass;
                //oCell.Controls.Add(lbl);

                ddl.SelectedIndex = PageIndex;
                oCell.Controls.Add(ddl);
                Label lbl2 = new Label();
                //lbl2.Text = "&nbsp;of&nbsp;" + PageCount.ToString() + "&nbsp;";

                lbl2.CssClass = ddl.CssClass;
                //oCell.Controls.Add(lbl2);
                oRow.Cells.Add(oCell);

                oCell = new TableCell();
                oCell.Controls.Add(nextLink);
                oRow.Cells.Add(oCell);

                oCell = new TableCell();
                oCell.Controls.Add(lastLink);
                oRow.Cells.Add(oCell);

                oCell = new TableCell();
                oCell.Width = Unit.Percentage(99);
                oCell.HorizontalAlign = HorizontalAlign.Right;
                oCell.Controls.Add(lbl);

                oRow.Cells.Add(oCell);

                if (UseCustom)
                {
                    base.FooterRow.Cells[0].Controls.Add(oTab);
                    base.FooterRow.Cells[0].HorizontalAlign = HorizontalAlign.Left;
                }
                else
                {
                    e.Row.Cells[0].Controls.Add(oTab);
                    e.Row.Cells[0].HorizontalAlign = HorizontalAlign.Left;
                }
            }
        }



        protected override void OnDataBound(EventArgs e)
        {
            base.OnDataBound(e);

            if (UseCustom)
            {
                BuildPageControl(PageCount2, null);
            }
        }


        protected override void OnRowCreated(GridViewRowEventArgs e)
        {
            if (!UseCustom)
            {
                if (e.Row.RowType == DataControlRowType.Pager)
                {
                    BuildPageControl(PageCount, e);
                }
            }
        }


        protected void lastLink_Click(object sender, ImageClickEventArgs e)
        {
            ddl.SelectedIndexChanged -= new EventHandler(ddl_SelectedIndexChanged);
            //ddl.SelectedValue = PageCount2.ToString();
            ddl.SelectedValue = PageCount.ToString();
            ddl.SelectedIndexChanged += new EventHandler(ddl_SelectedIndexChanged);
            //base.OnPageIndexChanging(new GridViewPageEventArgs(PageCount2));
            base.OnPageIndexChanging(new GridViewPageEventArgs(PageCount));
        }



        protected void nextLink_Click(object sender, ImageClickEventArgs e)
        {
            int PageID = Convert.ToInt32(ddl.SelectedItem.Value);
            ddl.SelectedIndexChanged -= new EventHandler(ddl_SelectedIndexChanged);
            ddl.SelectedValue = (PageID + 1).ToString();
            ddl.SelectedIndexChanged += new EventHandler(ddl_SelectedIndexChanged);
            base.OnPageIndexChanging(new GridViewPageEventArgs(PageIndex + 1));
        }



        protected void prevLink_Click(object sender, ImageClickEventArgs e)
        {
            int PageID = Convert.ToInt32(ddl.SelectedItem.Value);
            ddl.SelectedIndexChanged -= new EventHandler(ddl_SelectedIndexChanged);
            ddl.SelectedValue = (PageID - 1).ToString();
            ddl.SelectedIndexChanged += new EventHandler(ddl_SelectedIndexChanged);
            base.OnPageIndexChanging(new GridViewPageEventArgs(PageIndex - 1));
        }



        protected void firstLink_Click(object sender, ImageClickEventArgs e)
        {
            ddl.SelectedIndexChanged -= new EventHandler(ddl_SelectedIndexChanged);
            ddl.SelectedIndex = 0;
            ddl.SelectedIndexChanged += new EventHandler(ddl_SelectedIndexChanged);
            base.OnPageIndexChanging(new GridViewPageEventArgs(0));
        }



        protected void ddl_SelectedIndexChanged(object sender, EventArgs e)
        {
            DropDownList ddlPager = (DropDownList)sender;
            int PageID = Convert.ToInt32(ddlPager.SelectedItem.Value);
            PageID = PageID - 1;
            base.OnPageIndexChanging(new GridViewPageEventArgs(PageID));
        }
    }

}